//拷贝文件
const CopyWebpackPlugin = require("copy-webpack-plugin");
//引用webpack
const webpack = require('webpack')
    // 把css文件单独抽取出来 实现css的按需加载
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// 压缩css代码
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
//压缩js代码
const TerserJSPlugin = require('terser-webpack-plugin');
//引用合并函数
const merge = require("webpack-merge")
    //引用公共代码
const common = require('./webpack.common')

const path = require('path')

module.exports = merge.merge(common, {
    mode: "production", //模式
    devtool: "none", //工具
    output: {
        filename: "[name]-[contenthash:8].bundle.js"
    },
    optimization: { // 优化项
        minimizer: [
            new TerserJSPlugin(), // 压缩js
            new OptimizeCssAssetsPlugin() // 压缩css,使用了这个插件后，js的压缩就不生效了，所以安装terser-webpack-plugin压缩js
        ],
        usedExports: true, //除去未引用代码
        minimize: true, //压缩代码
        concatenateModules: true,
        splitChunks: {
            chunks: "all" //提取公共文件
        }
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name]-[contenthash:8].main.css', // 抽取出来的css文件名main.css, css/main.css：抽取出来的文件放到css文件下叫mian.css
        }),
        new webpack.BannerPlugin('最终解释权归本人所有'), //加上版权所属
        new CopyWebpackPlugin({ //拷贝静态文件
            patterns: [{
                    from: path.join(__dirname, '../public/favicon.ico'),
                    to: '.'
                },
                {
                    from: path.join(__dirname, '../public/cloud.jpeg'),
                    to: '.'
                }

            ],
            options: {
                concurrency: 100,
            },
        })
    ]
})